package com.jiayang.hhotel.presenter;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.InfoModel;
import com.jiayang.hhotel.bean.RoomThingModel;
import com.jiayang.hhotel.net.Api;
import com.jiayang.hhotel.ui.RoomThingActivity;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.ToastUtil;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by Administrator on 2017/5/19 0019.
 * 客房物品逻辑类
 */

public class RoomThingPresenter extends XPresent<RoomThingActivity> {

    /**
     * 请求物品列表
     * @param mpLanguage 语言
     * @param mpRoom 房号
     * @param mpType 1-酒水；2-洗衣；3-物品；4-其他（这里固定为3）
     */
    public void asyncListLoad(int mpLanguage,String mpRoom,int mpType){
        Api.getHomeSer().guestThingList(Constant.PHONE,mpLanguage,mpRoom,mpType)
                .compose(XApi.<RoomThingModel>getApiTransformer())
                .compose(XApi.<RoomThingModel>getScheduler())
                .compose(getV().<RoomThingModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<RoomThingModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV());
                        getV().sealPic(error.getType());
                    }

        @Override
        public void onNext(RoomThingModel Model) {
            switch (Model.getState()){
                case 9://请求成功
                    getV().setAdapter(Model.getPHKTransListDto());
                    break;
                case -1://失败
                    getV().sealPic(-1);
                    break;
                case 1002://没有数据
                    getV().sealPic(1002);
                    break;
            }
        }
    });
    }

    /**
     * 客房物品保存
     * @param mpLanguage 语言
     * @param mpIsmanager -0-游客(客人损失)、1-商家（客房收费）
     * @param mpRoom 房号
     * @param mpHKTrans HKMenu，Quantity2（消费代码,遗损量,消费代码,遗损量....）
     * @param mpType 1-酒水；2-洗衣；3-物品；4-其他（这里固定为3）
     */
    public void asyncSave(int mpLanguage,int mpIsmanager,String mpRoom,String mpHKTrans,int mpType){
        Api.getHomeSer().guestThingSave(Constant.PHONE,mpLanguage,mpIsmanager,mpRoom,mpHKTrans,mpType)
                .compose(XApi.<InfoModel>getApiTransformer())
                .compose(XApi.<InfoModel>getScheduler())
                .compose(getV().<InfoModel>bindToLifecycle())
                .subscribe(new ApiSubscriber<InfoModel>() {
                    @Override
                    protected void onFail(NetError error) {
                        CommonUtil.dismissProgressDialog(getV());
                        ToastUtil.Toast(getV().getString(R.string.net_error),getV());
                    }

                    @Override
                    public void onNext(InfoModel Model) {
                        CommonUtil.dismissProgressDialog(getV());
                        LogUtil.showLog("model="+Model.toString());
                        switch (Model.getState()){
                            case 9://请求成功
                                ToastUtil.Toast(getV().getString(R.string.already_submit),getV());
                                break;
                            default://其他错误
                                ToastUtil.Toast(getV().getString(R.string.error_code)+Model.getState()+getV().getString(R.string.space)+getV().getString(R.string.service_error_contract_manager),getV());
                                break;
                        }
                    }
                });
    }
}
